<?php
//	$url="localhost";
//	$user_name="root";
//	$db_password="password";
//	$db_name="adaptive_learning";
//	$con=mysqli_connect($url, $user_name, $db_password, $db_name);
	include 'db_connect.php';

	$s_id = $_GET["matric"];
	$module = $_GET["module"];
	$topic = $_GET["topic"];
	$difficulty = $_GET["difficulty"];

	// Check connection
	if ($con) {
		$result = mysqli_query($con,"SELECT * FROM Question WHERE t_id = " . $topic . " AND m_code = '" . $module . "' AND diff_level = 
(SELECT min(diff_level) FROM Question WHERE t_id = " . $topic . " AND m_code = '" . $module . "' AND (diff_level >= " . $difficulty . " OR diff_level = (SELECT max(diff_level) from Question where t_id = " . $topic . " AND m_code = '" . $module . "'))) ORDER BY RAND() LIMIT 1");
//		$result = mysqli_query($con,"SELECT * FROM Question WHERE diff_level >= " . $difficulty . " ORDER BY diff_level LIMIT 1");
		$row = mysqli_fetch_array($result);
		$question = array( "q_id" => $row['id'], "content" => $row['content'], "choice_a" => $row['choice_a'], "choice_b" => $row['choice_b'], "choice_c" => $row['choice_c'],
			 "choice_d" => $row['choice_d'], "diff_level" => $row['diff_level'], "answer" => $row['answer']);

		echo json_encode($question);
	}
?>